# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup as bs
import requests
import re
from pyfzf.pyfzf import FzfPrompt
import os

print("Загрузка....")

f = [i.strip('\n').split(',') for i in open('subscribe.txt')]


chanel_list = []

for k in f:
    h = (k[0])
    pre_source = requests.get(h)
    pre_source.encoding = 'utf-8'
    pre_soup = bs(pre_source.text, 'html.parser')
    pre_name = pre_soup.select("span")
    pre_name1 = pre_name[1]
    pre_canell = (pre_name1.text)
    chanel_list.append(pre_canell)

fzf = FzfPrompt()



target_chanel=fzf.prompt(chanel_list)
print(target_chanel[0])
nomber_chanel = (chanel_list.index(target_chanel[0]))


url = f[nomber_chanel]

end_url = url[0]



source = requests.get(end_url)

source.encoding = 'utf-8' 


soup = bs(source.text, 'html.parser')


name = soup.select("span")
name1 = name[1]
canell = (name1.text)



spisokNAME=[]
z=0
for title in soup.select("div p"):
    kkk = title.find('a')



    
    if kkk is not None:
        xxx = kkk
        ggg=xxx.text
        if re.search(canell, ggg):
            a = 1
        else:
            z=z+1

            if z >= 2:

                spisokNAME.append(ggg) 
                

target=fzf.prompt(spisokNAME)

print(target[0])
nomber = (spisokNAME.index(target[0]))


spisok=[]

for a in soup.find_all('a', href=True):
    link=(a['href'])
    
    if re.search(r'com/watch\b', link):   
        spisok.append(link) 



end_target = spisok[nomber]

comand = "mpv " + end_target

os.system(comand)           
                
